<template>
    <div class="main-container"  :class="wrap?'main-wrap-container':'main-no-wrap-container'" id="search-mixin-id">
    <main-content :title="contentTitle" >
        <div slot="search"  v-if="showSearchBox">
          <SearchBox 
            v-if="showSearchBox"
            :searchParamsList="searchParamsList" 
            :searchParam.sync="searchParam" 
            @searchBoxParams="searchClick" 
            @searchReset="searchReset" 
            ></SearchBox> 
        </div>
        <div slot="table">
        <!-- 在此处放置表格内容 -->
            <avue-crud ref="apply_sealTable" :data="listData" @selection-change="selectionChange" id="printTable" :option="option" stripe :page.sync="page"
                       @on-load="onPageLoad"
                       @row-dblclick="handleRowDBLClick"
                       @sort-change="sortChange" 
                       @current-row-change="handleCurrentRowChange" >
                <template slot-scope="scope" slot="menu">
		  			   <template v-if="scope.row.flowStatus&&scope.row.flowStatus==2">
						  <span v-if="hasBtns('createFormFlow') && hasButtonPermission('createFormFlow')" class="form-span-button">{{$t("workFlow.WFChange.approval")}}</span>
						  <el-button type="opr-primary" size="mini"  icon="el-icon-view"  v-if="hasBtns('view')&&hasButtonPermission('view')" @click="viewForm(scope.row)">{{$t("crud.viewBtn")}}</el-button>
		  		 	   </template>
		  			   <template v-else-if="scope.row.flowStatus&&scope.row.flowStatus==3">
						  <span v-if="hasBtns('createFormFlow') && hasButtonPermission('createFormFlow')" class="form-span-button">{{$t("forme.editModal.approvalComplete")}}</span>
						  <el-button type="opr-primary" size="mini"  icon="el-icon-view"  v-if="hasBtns('view')&&hasButtonPermission('view')" @click.stop="viewForm(scope.row)">{{$t("crud.viewBtn")}}</el-button>
						  <el-button type="opr-primary" size="mini"  icon="el-icon-edit" v-if="hasBtns('modify')&&hasButtonPermission('modify')" @click.stop="editForm(scope.row)">{{$t("crud.editBtn")}}</el-button>
						  <el-button type="opr-primary" size="mini"  icon="el-icon-delete" v-if="hasBtns('delete')&&hasButtonPermission('delete')" @click.stop="deleteForm(scope.row)">{{$t("crud.delBtn")}}</el-button>
		  			   </template>
		  			   <template v-else>
			  			  <el-button type="opr-primary" size="mini" icon="el-icon-guide" v-if="scope.row.flowStatus&&scope.row.flowStatus==1&&hasBtns('createFormFlow') && hasButtonPermission('createFormFlow')" @click="CreateFormFlow(scope.row)">{{$t("forme.editModal.initiateApproval")}}</el-button>
			  			  <el-button type="opr-primary" size="mini" icon="el-icon-guide" v-if="hasBtns('createDesignFlow') && hasButtonPermission('createDesignFlow')" @click="CreateDesignFlow(scope.row)">{{$t("forme.editModal.createFormFlowBtn")}}</el-button>
			  			  <el-button type="opr-primary" size="mini"  icon="el-icon-view"  v-if="hasBtns('view')&&hasButtonPermission('view')" @click="viewForm(scope.row)">{{$t("crud.viewBtn")}}</el-button>
			  			  <el-button type="opr-primary" size="mini"  icon="el-icon-edit" v-if="hasBtns('modify')&&hasButtonPermission('modify')" @click="editForm(scope.row)">{{$t("crud.editBtn")}}</el-button>
			  			  <el-button type="opr-primary" size="mini"  icon="el-icon-delete" v-if="hasBtns('delete')&&hasButtonPermission('delete')" @click="deleteForm(scope.row)">{{$t("crud.delBtn")}}</el-button>
					   </template>
                </template>
                 <template slot="menuLeft">
                     <el-button type="primary"  v-if="hasButtonPermission('add')" icon="el-icon-plus" size="small" @click="openForm()">{{$t("crud.addBtn")}}</el-button>
                     <el-button type="primary"  v-if="hasButtonPermission('batchDelete')" icon="el-icon-delete" size="small" @click="batchDelete()">{{$t("forme.editModal.batchDeletionBtn")}}</el-button>
                     <el-button type="primary"  v-if="hasButtonPermission('print')" icon="el-icon-printer" size="small" @click="printForm">{{$t("editorModule.print")}}</el-button>
                </template>
            </avue-crud>
            <el-dialog title="表单" :append-to-body="true" :fullscreen="true" :visible.sync="flag"  :class="isWorkflowForm ? 'work-form-dialog' : ''" :show-close="!isWorkflowForm" >
                <apply_sealForm ref="xtForm"  v-if="flag"  :isEdit="isEdit" :disabled="disabled" @onChange="dialogChange" :keyValue="keyValue"></apply_sealForm>
            </el-dialog>
        </div>
    </main-content>
    <createFlow v-if="isCreateFlow" formType="2" rowPrimaryKey="F_Id" :UrlAddress="this.$options.name" :createFlowType="createFlowType" :row="scopeRow" :scheme="scheme" @closedCreateFlow="closedCreateFlow"></createFlow>
    </div>
</template>
<script>
      import createFlow from '@/components/formInitiationProcess/createFlow';
      import schemeMixins from '@/components/formInitiationProcess/schemeMixins';
     import MainFixed from "@/page/main/MainFixed";
      import authorizeMixin from "@/mixins/authorize";
      import tableOptionMixin from "@/mixins/tableOption"; //table 高度计算
      import tableSearchMixin from "@/mixins/tableSearch"; //table 搜索 
     import MainContent from "@/page/main/MainContent";
      import request from '@/router/axios';
      import { baseUrl } from '@/config/env';
      import { getOrder } from '@/util/util';
      import apply_sealForm from './apply_sealForm';
     export default {
         name: "apply_seal",
         mixins: [authorizeMixin,tableOptionMixin,tableSearchMixin,schemeMixins],
         components: { MainContent, MainFixed,apply_sealForm,createFlow },
         data() {
              return {
				 	  btns:[
				 	     'refresh',
				 	     'add',
				 	     'modify',
				 	     'delete',
				 	     'batchDelete',
				 	     'print'
                   ],
                   page: {
                       pageSize: 20,
                       total: 0,
                       currentPage: 1,
                       layout: "total, prev, pager, next, jumper"
                   },
                   ids: '',
                   listData: [],
                   keyValue: '',
                   flag:false,
                   isEdit: true, // 查看-不显示按钮,
                   disabled: false, // 查看-禁用input,
                   isWorkflowForm:false,//默认值 false
                   defaultOrderfield:'',//默认排序字段
                   searchParam: {},
                   defaultProps: {
                       children: 'children',
                       label: ''
                   },
                   option:{
                       rowKey:'F_Id',
                       highlightCurrentRow: true,
                       stripe: true,
                       addBtn: false,
                       tip:false,
                       menuWidth: 320,
                       border: true,
                       page: true,
                       selection: true,
                       index: true,
                       menu: false,
                       indexLabel: this.$t('custmerForm.tableNumber'),
                       align: 'center',
                       menuAlign: 'left',
                       delBtn: false,
                       editBtn: false,
                       column: [
                           {
                               sortable: true,
                               label: this.$t("申请人ID"),
                               prop: 'user_id',
                               overHidden: true,
                           },
                           {
                               sortable: true,
                               label: this.$t("申请人名称"),
                               prop: 'user_name',
                               overHidden: true,
                           },
                           {
                               sortable: true,
                               label: this.$t("申请时间"),
                               prop: 'apply_time',
                               overHidden: true,
                           },
                           {
                               sortable: true,
                               label: this.$t("职级ID"),
                               prop: 'job_id',
                               overHidden: true,
                           },
                           {
                               sortable: true,
                               label: this.$t("职级名称"),
                               prop: 'job_name',
                               overHidden: true,
                           },
                           {
                               sortable: true,
                               label: this.$t("部门ID"),
                               prop: 'dept_id',
                               overHidden: true,
                           },
                           {
                               sortable: true,
                               label: this.$t("部门名称"),
                               prop: 'dept_name',
                               overHidden: true,
                           },
                           {
                               sortable: true,
                               label: this.$t("角色ID"),
                               prop: 'role_id',
                               overHidden: true,
                           },
                           {
                               sortable: true,
                               label: this.$t("角色名称"),
                               prop: 'role_name',
                               overHidden: true,
                           },
                           {
                               sortable: true,
                               label: this.$t("地区"),
                               prop: 'area',
                               overHidden: true,
                           },
                           {
                               sortable: true,
                               label: this.$t("经办人ID"),
                               prop: 'transactor_id',
                               overHidden: true,
                           },
                           {
                               sortable: true,
                               label: this.$t("经办人"),
                               prop: 'transactor_name',
                               overHidden: true,
                           },
                           {
                               sortable: true,
                               label: this.$t("日期"),
                               prop: 'transaction_time',
                               overHidden: true,
                           },
                           {
                               sortable: true,
                               label: this.$t("是否借出"),
                               prop: 'F_isloan',
                               overHidden: true,
                           },
                           {
                               sortable: true,
                               label: this.$t("印章公司选择"),
                               prop: 'seal_company',
                               overHidden: true,
                           },
                           {
                               sortable: true,
                               label: this.$t("印章类型"),
                               prop: 'seal_type',
                               overHidden: true,
                           },
                           {
                               sortable: true,
                               label: this.$t("印章公司选择(借出)"),
                               prop: 'loan_seal_company',
                               overHidden: true,
                           },
                           {
                               sortable: true,
                               label: this.$t("事由"),
                               prop: 'reason',
                               overHidden: true,
                           },
                           {
                               sortable: true,
                               label: this.$t("借出时间"),
                               prop: 'out_time',
                               overHidden: true,
                           },
                           {
                               sortable: true,
                               label: this.$t("预计归还时间"),
                               prop: 'return_time',
                               overHidden: true,
                           },
                           {
                               sortable: true,
                               label: this.$t("借出事由"),
                               prop: 'reason_type',
                               overHidden: true,
                           },
                           {
                               sortable: true,
                               label: this.$t("备注"),
                               prop: 'remark',
                               overHidden: true,
                           },
                           {
                               sortable: true,
                               label: this.$t("是否生效"),
                               prop: 'F_IsEffective',
                               overHidden: true,
                           },
                       ]
                   }
              }
                   },
                   created() {
                       //是否有编辑 查看 删除 发起流程 发起临时流程权限 无则为操作列不显示
                       let oprBtns = ['modify','view','delete','createFormFlow','createDesignFlow'];
                       let intersection = this.btns.filter((v) => oprBtns.includes(v)); 
                       if(intersection.length>0){
                       	this.option.menu =  true
                       }else{
                       	this.option.menu = false
                       }
                       let querySearchData = [
                       	{ 
                       		name: '申请人ID', 
                       		DateField: '', 
                       		field: 'user_id', 
                       		tag: 'xjr-select-user', 
                       		lgmark: '', 
                       		__organize__: true, 
                       		__info__: false, 
                       		__type__: 'user', 
                       	}, 
                       	{ 
                       		name: '申请人名称', 
                       		DateField: '', 
                       		field: 'user_name', 
                       		tag: 'el-input', 
                       		lgmark: '', 
                       	}, 
                       	{ 
                       		name: '申请时间', 
                       		DateField: '', 
                       		field: 'apply_time', 
                       		tag: 'el-input', 
                       		lgmark: '', 
                       	}, 
                       	{ 
                       		name: '职级ID', 
                       		DateField: '', 
                       		field: 'job_id', 
                       		tag: 'el-input', 
                       		lgmark: '', 
                       	}, 
                       	{ 
                       		name: '职级名称', 
                       		DateField: '', 
                       		field: 'job_name', 
                       		tag: 'el-input', 
                       		lgmark: '', 
                       	}, 
                       	{ 
                       		name: '部门ID', 
                       		DateField: '', 
                       		field: 'dept_id', 
                       		tag: 'avue-input-tree', 
                       		lgmark: '', 
                       		__organize__: true, 
                       		__info__: false, 
                       		__type__: 'department', 
                       	}, 
                       	{ 
                       		name: '部门名称', 
                       		DateField: '', 
                       		field: 'dept_name', 
                       		tag: 'el-input', 
                       		lgmark: '', 
                       	}, 
                       	{ 
                       		name: '角色ID', 
                       		DateField: '', 
                       		field: 'role_id', 
                       		tag: 'el-input', 
                       		lgmark: '', 
                       	}, 
                       	{ 
                       		name: '角色名称', 
                       		DateField: '', 
                       		field: 'role_name', 
                       		tag: 'el-input', 
                       		lgmark: '', 
                       	}, 
                       	{ 
                       		name: '地区', 
                       		DateField: '', 
                       		field: 'area', 
                       		tag: 'el-input', 
                       		lgmark: '', 
                       	}, 
                       	{ 
                       		name: '经办人ID', 
                       		DateField: '', 
                       		field: 'transactor_id', 
                       		tag: 'xjr-select-user', 
                       		lgmark: '', 
                       		__organize__: true, 
                       		__info__: false, 
                       		__type__: 'user', 
                       	}, 
                       	{ 
                       		name: '经办人', 
                       		DateField: '', 
                       		field: 'transactor_name', 
                       		tag: 'el-input', 
                       		lgmark: '', 
                       	}, 
                       	{ 
                       		name: '日期', 
                       		DateField: '', 
                       		field: 'transaction_time', 
                       		tag: 'el-date-picker', 
                       		lgmark: '', 
"type": "date",
"format": "yyyy-MM-dd",
"value-format": "yyyy-MM-dd",
                       	}, 
                       	{ 
                       		name: '是否借出', 
                       		DateField: '', 
                       		field: 'F_isloan', 
                       		tag: 'el-select', 
                       		lgmark: '', 
                       		dataType: 'dataItem', 
                       		dataItem: '10020', 
                       	}, 
                       	{ 
                       		name: '印章公司选择', 
                       		DateField: '', 
                       		field: 'seal_company', 
                       		tag: 'el-select', 
                       		lgmark: '', 
                       		dataType: 'dataItem', 
                       		dataItem: 'stamp_company', 
                       	}, 
                       	{ 
                       		name: '印章类型', 
                       		DateField: '', 
                       		field: 'seal_type', 
                       		tag: 'el-select', 
                       		lgmark: '', 
                       		dataType: 'dataItem', 
                       		dataItem: 'type_seal', 
                       	}, 
                       	{ 
                       		name: '印章公司选择(借出)', 
                       		DateField: '', 
                       		field: 'loan_seal_company', 
                       		tag: 'el-select', 
                       		lgmark: '', 
                       		dataType: 'dataItem', 
                       		dataItem: 'stamp_company', 
                       	}, 
                       	{ 
                       		name: '事由', 
                       		DateField: '', 
                       		field: 'reason', 
                       		tag: 'el-input', 
                       		lgmark: '', 
                       	}, 
                       	{ 
                       		name: '借出时间', 
                       		DateField: '', 
                       		field: 'out_time', 
                       		tag: 'el-date-picker', 
                       		lgmark: '', 
"type": "date",
"format": "yyyy-MM-dd",
"value-format": "yyyy-MM-dd",
                       	}, 
                       	{ 
                       		name: '预计归还时间', 
                       		DateField: '', 
                       		field: 'return_time', 
                       		tag: 'el-date-picker', 
                       		lgmark: '', 
"type": "date",
"format": "yyyy-MM-dd",
"value-format": "yyyy-MM-dd",
                       	}, 
                       	{ 
                       		name: '借出事由', 
                       		DateField: '', 
                       		field: 'reason_type', 
                       		tag: 'el-select', 
                       		lgmark: '', 
                       		dataType: 'dataItem', 
                       		dataItem: 'lend_reason', 
                       	}, 
                       	{ 
                       		name: '备注', 
                       		DateField: '', 
                       		field: 'remark', 
                       		tag: 'el-input', 
                       		lgmark: '', 
                       	}, 
                       	{ 
                       		name: '是否生效', 
                       		DateField: '', 
                       		field: 'F_IsEffective', 
                       		tag: 'el-input', 
                       		lgmark: '', 
                       	}, 

                       ]
                       this.getSystemSearchList(querySearchData);
                       if(this.btns.length>0){
                       	let tempBtns =  this.btns.concat(oprBtns).filter((v) => this.btns.includes(v) && !oprBtns.includes(v));
                       	if(tempBtns.length>0){
                       		this.searchBtns  = [...new Set([...tempBtns])];
                       	}
                       }
                   },
                   methods: {
                         handleRowDBLClick (row, event) {
                         	if(this.hasBtns('modify')&&this.hasButtonPermission('modify')&&this.openDoubleClickEdit){
                         		this.editForm(row)
                         	}
                         },
                         handleCurrentRowChange(row){
                         	this.$refs.apply_sealTable.toggleSelection([row]);
                         },
                         printForm() {
                         	let element = window.document.getElementById("printTable");
                         	setTimeout(() => {
                         	  html2canvas(element, {
                         		backgroundColor: null,
                         		useCORS: true,
                         		windowHeight: document.body.scrollHeight
                         	  }).then(canvas => {
                         		const url = canvas.toDataURL();
                         		this.printImg = url;
                         		printJS({
                         		  printable: url,
                         		  type: "image",
                         		  documentTitle: '打印',
                         		  scanStyles: false,
                         		  repeatTableHeader: false,
                         		  header: null
                         		});
                          	  });
                         	}, 1);
                           },
                         hasBtns(encode){
                         	return this.btns.includes(encode);
                         },
                         /**发起流程 */
                         CreateDesignFlow(val) {
                            this.createFlowType = 0;
                            this.scopeRow = val;
                            this.changeSystemSchemeInfo();
                         },
                         CreateFormFlow(val) {
                            this.createFlowType = 1;
                            this.scopeRow = val;
                            this.isCreateFlow = true;
                         },
                         /* 获取公司用户数据 */
                         getListData() {
                         	if(!this.searchParam.order){
                         		this.searchParam.orderfield = this.defaultOrderfield;
                         	}
                            let params = this.changeSearchParams();
                            request({
                                url: baseUrl + '/apply_seal',
                                method: 'get',
                                params: params
                            }).then(res =>{
                                this.listData = res.data.data.Rows
                                this.page.total = res.data.data.Total;
                            })
                         },
                         /* 页面加载 */
                         onPageLoad(page) {
                            this.searchParam.limit = page.currentPage;
                            this.searchParam.size = page.pageSize;
                            this.getListData();
                         },
                          /*查看*/
                         viewForm(val) {
                           this.flag = true;
                           this.isEdit = false;
                           this.disabled = true;
                           this.keyValue = val.F_Id; 
                         },
                          /* 编辑 */
                         editForm(val) {
                           this.flag = true; 
                           this.isEdit = true;
                           this.disabled = false;
                           this.keyValue = val.F_Id; 
                         },
                          /* 打开表单 */
                         openForm(){
							  if(!this.isWorkflowForm){
                             //不处理工作流表单时候，表单类型重置
                             window.localStorage.setItem('systemFormType',0);
                           }
                           this.keyValue = null; 
                           this.flag = true; 
                           this.isEdit = true; 
                         },
                          /* 删除 */
                         deleteForm(val) {
                            this.$confirm(this.$t("deletePrompt.promptContent"), this.$t("tip"), {
                              confirmButtonText: this.$t("buttonModule.modalSureBtn"),
                              cancelButtonText: this.$t("editorModule.cancel"),
                              type: "warning",
                             }).then(() => {
                                 request({
                                     url: baseUrl + "/apply_seal/" + val.F_Id,
                                     method: "delete",
                                 }).then((res) => {
                                     this.getListData()
                                     this.$notify({
                                      title: this.$t("Notice.hintSuccess"),
                                      message: this.$t("deletePrompt.successDeleted"),
                                      type: "success",
                                     });
                                 });
                             });
                         },
                          /* 分页 */
                         sortChange(data) {
                         	if(data.order){
                         		let order= getOrder(data.order);
                         		this.searchParam.order = order;
                         		this.searchParam.orderfield = data.prop;
                         	}else{
                         		this.searchParam.order = asc;
                         		this.searchParam.orderfield = this.defaultOrderfield;
                         	}
                         	this.getListData()
                         },
                          /* 弹窗回调 */
                          dialogChange(b){
                              this.flag = false;
                              this.$nextTick(()=>{
                              this.getListData();
                         })},
                          /* 查询 */
                           searchClick(params) {
                            this.page.currentPage=1;
                            this.searchParam.limit = 1;
                            this.searchParam = params;
                            this.getListData()
                         },
                           /* 查询表单重置 */
                           searchReset() {
                            for (const key in this.searchParam) {
                                 if(this.searchParam[key]){
                                     this.searchParam[key] = null;
                                 }
                            }
                             this.getListData();
                         },
                           selectionChange(list){
                             this.ids = list.map((x) => x.F_Id).join(',');
                           },
                           batchDelete() {
                            if (!this.ids || this.ids.split(",").length === 0) {
                                 this.$message.error(this.$t("deletePrompt.noSelected"));
                                 return;
                           }
                             this.$confirm(this.$t("deletePrompt.promptContent"), this.$t("tip"), {
                               confirmButtonText: this.$t("buttonModule.modalSureBtn"),
                               cancelButtonText: this.$t("editorModule.cancel"),
                               type: "warning",
                             }).then(() => {
                               request({
                                 url: baseUrl + "/apply_seal/" + this.ids,
                                 method: "delete",
                               }).then((res) => {
                                 this.getListData();
                                 this.$notify({
                                   title: this.$t("Notice.hintSuccess"),
                                   message: this.$t("deletePrompt.successDeleted"),
                                   type: "success",
                                 });
                               });
                             });
                           },
         }
     }
</script>
<style scoped>
.searchBox {
   width: calc(100% - 218px);
   display: inline-block;
}
.main-container{
   height: 100%!important;
   padding: 0 8px!important;
}
div /deep/ .custom-tree-node span{
   font-size: 14px;
   padding-left: 10px;
}
div /deep/ .custom-tree-node i{
   font-size: 12px;
}
</style>
